package edu.mis.modules.voucher;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Db;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import edu.mis.model.Voucher;
import edu.mis.model.Voucheritem;

public class VoucherController extends Controller {
	public void index() {
		render("voucher.html");
	}

	/**
	 * 获取所有的凭证
	 */
	public void list() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Page<Voucher> data = Voucher.dao.paginate(page, rows);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}
	
	/**
	 * 获取当月凭证
	 */
	public void voucher() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		String date1 = year+"-"+month+"-01";
		String date2 = year+"-"+month+"-31";
		String voucherId = "";
		String maker = "";
		Page<Voucher> data = Voucher.dao.searchVoucher(page, rows, date1, date2, voucherId, maker);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}
	
	/**
	 * 获得最新凭证号
	 */
	public void getVoucherId(){
		List<Voucher> list=Voucher.dao.find("select * from voucher order by voucherId desc limit 1 ;");
		if(list.size()==0){
			setAttr("voucherId",0);
		}else{
			Voucher voucher=list.get(0);
			setAttr("voucherId",voucher.getVoucherId());
		}
		renderJson();
	}
	
	/**
	 * 查询凭证
	 */
	public void searchVoucher() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		String date1 = getPara("date1");
		String date2 = getPara("date2");
		String voucherId = getPara("voucherId");
		String maker = getPara("maker");
		Page<Voucher> data = Voucher.dao.searchVoucher(page, rows, date1, date2, voucherId, maker);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}

	/**
	 * 新增凭证
	 * 
	 * @throws Exception
	 */
	public void add() throws Exception {
		String jsonString = new String(getPara("voucher"));
		JSONObject obj = JSON.parseObject(jsonString);
		Voucher voucher = obj.getObject("voucher", Voucher.class);
		Voucheritem voucherItem = null;
		if (voucher.save()) {
			JSONArray array = obj.getJSONArray("voucherItem");
			for (int i = 0, len = array.size(); i < len; i++) {
				voucherItem = new Voucheritem();
				JSONObject temp = array.getJSONObject(i);
				int captionId = temp.getInteger("captionId");
				voucherItem.setVoucherId(voucher.getVoucherId());
				voucherItem.setSummary(temp.getString("summary"));
				voucherItem.setCaptionId(captionId);
				voucherItem.setDebitAmount(temp.getDouble("debitAmount"));
				voucherItem.setCreditAmount(temp.getDouble("creditAmount"));
				if (voucherItem.save()) {
					//设置科目已被使用
					Db.update("update account_caption set isUse = 1 where captionId="+captionId);
					setAttr("success", true);
					setAttr("msg", "新增凭证成功!");
				} else {
					setAttr("success", false);
					setAttr("msg", "新增凭证失败!");
				}
			}
		} else {
			setAttr("success", false);
			setAttr("msg", "新增凭证失败!");
		}
		renderJson();
	}

	/**
	 * 获得凭证明细
	 */
	public void getVoucherItem() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		String voucherId = getPara("voucherId");
		Page<Voucheritem> data = Voucheritem.dao.getVoucherItem(page, rows, voucherId);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}
	
	/**
	 * 编辑凭证明细
	 * 
	 * @throws Exception
	 */
	public void edit() throws Exception {
		String jsonString = new String(getPara("voucher"));
		JSONObject obj = JSON.parseObject(jsonString);
		String voucherId = obj.getJSONObject("voucher").getString("voucherId");
		List<Voucheritem> list=Voucheritem.dao.find("select * from voucherItem where voucherId="+voucherId);
		Voucheritem voucherItem = null;
		JSONArray array = obj.getJSONArray("voucherItem");
		int len = array.size();
		int length = list.size();
		if(len==length){ //要编辑的明细条目与数据库条目相同
			for (int i = 0; i < len; i++) {
				voucherItem = list.get(i);
				JSONObject temp = array.getJSONObject(i);
				voucherItem.setSummary(temp.getString("summary"));
				voucherItem.setCaptionId(temp.getInteger("captionId"));
				voucherItem.setDebitAmount(temp.getDouble("debitAmount"));
				voucherItem.setCreditAmount(temp.getDouble("creditAmount"));
				if (voucherItem.update()) {
					setAttr("success", true);
					setAttr("msg", "编辑凭证明细成功!");
				} else {
					setAttr("success", false);
					setAttr("msg", "编辑凭证明细失败!");
				}
			}
		}else if(len > length){ //要编辑的明细条目大于数据库条目
			boolean flag=false;
			for (int i = 0; i < length; i++) {
				voucherItem = list.get(i);
				JSONObject temp = array.getJSONObject(i);
				voucherItem.setSummary(temp.getString("summary"));
				voucherItem.setCaptionId(temp.getInteger("captionId"));
				voucherItem.setDebitAmount(temp.getDouble("debitAmount"));
				voucherItem.setCreditAmount(temp.getDouble("creditAmount"));
				if(voucherItem.update()){
					flag=true;
				}else{
					flag=false;
				}
			}
			for(int i=length;i<len;i++){
				voucherItem = new Voucheritem();
				voucherItem.setVoucherId(Integer.parseInt(voucherId));
				JSONObject temp = array.getJSONObject(i);
				voucherItem.setSummary(temp.getString("summary"));
				voucherItem.setCaptionId(temp.getInteger("captionId"));
				voucherItem.setDebitAmount(temp.getDouble("debitAmount"));
				voucherItem.setCreditAmount(temp.getDouble("creditAmount"));
				if(voucherItem.save()){
					flag=true;
				}else{
					flag=false;
				}
			}
			if(flag){
				setAttr("success", true);
				setAttr("msg", "编辑凭证明细成功!");
			}else{
				setAttr("success", false);
				setAttr("msg", "编辑凭证明细失败!");
			}
		}else if(len < length){  //要编辑的明细条目小于数据库条目
			boolean flag=false;
			for (int i = 0; i < len; i++) {
				voucherItem = list.get(i);
				JSONObject temp = array.getJSONObject(i);
				voucherItem.setSummary(temp.getString("summary"));
				voucherItem.setCaptionId(temp.getInteger("captionId"));
				voucherItem.setDebitAmount(temp.getDouble("debitAmount"));
				voucherItem.setCreditAmount(temp.getDouble("creditAmount"));
				if(voucherItem.update()){
					flag=true;
				}else{
					flag=false;
				}
			}
			for(int i=len;i<length;i++){
				voucherItem = list.get(i);
				if(Db.update("delete from voucherItem where id="+voucherItem.getId())<=0){
					flag=false;
				}else{
					flag=true;
				}
			}
			if(flag){
				setAttr("success", true);
				setAttr("msg", "编辑凭证明细成功!");
			}else{
				setAttr("success", false);
				setAttr("msg", "编辑凭证明细失败!");
			}
		}
		renderJson();
	}

}
